/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        std::vector<int> res;
        std::vector<TreeNode*> T;
        while(root || !T.empty()) {
            while(root) {
                T.push_back(root);
                root = root->left;
            }
            if (!T.empty()) {
                root = T.back();
                T.pop_back();
                res.push_back(root->val);
                root = root->right;
            }
        }
    return res;
    }
};